home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / hdf / unix / hdf3_2r2.lha / HDF3.2r2 / src / vsfld.c < prev   
Encoding:
C/C++ Source or Header  |  1992-10-28  |  10.4 KB  |  379 lines

  1. /***************************************************************************
  2. *
  3. *
  4. *                         NCSA HDF version 3.2r2
  5. *                            October 30, 1992
  6. *
  7. * NCSA HDF Version 3.2 source code and documentation are in the public
  8. * domain.  Specifically, we give to the public domain all rights for future
  9. * licensing of the source code, all resale rights, and all publishing rights.
  10. *
  11. * We ask, but do not require, that the following message be included in all
  12. * derived works:
  13. *
  14. * Portions developed at the National Center for Supercomputing Applications at
  15. * the University of Illinois at Urbana-Champaign, in collaboration with the
  16. * Information Technology Institute of Singapore.
  17. *
  18. * THE UNIVERSITY OF ILLINOIS GIVES NO WARRANTY, EXPRESSED OR IMPLIED, FOR THE
  19. * SOFTWARE AND/OR DOCUMENTATION PROVIDED, INCLUDING, WITHOUT LIMITATION,
  20. * WARRANTY OF MERCHANTABILITY AND WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE
  21. *
  22. ****************************************************************************
  23. */
  24.  
  25. #ifdef RCSID
  26. static char RcsId[] = "@(#)$Revision: 1.3 $";
  27. #endif
  28. /*
  29. $Header: /hdf/hdf/v3.2r2/src/RCS/vsfld.c,v 1.3 1992/10/23 00:14:11 koziol beta koziol $
  30.  
  31. $Log: vsfld.c,v $
  32.  * Revision 1.3  1992/10/23  00:14:11  koziol
  33.  * Changed all DFIstr*() and DFImem*() calls to HDstr*() and HDmem*() calls
  34.  * #ifdef'd out the macros Jason defined for Hopen, Hclose, etc. for Vsets
  35.  * Replaced Vset VFREESPACE and VGETSPACE calls with actual calls to HDfreespace
  36.  * and HDgetspace
  37.  * Added a MS-Windows lower lower for file I/O (which may not be completely working
  38.  *
  39.  * Revision 1.2  1992/10/12  18:11:51  koziol
  40.  * Updated for v3.2r2 release
  41.  *
  42.  * Revision 1.1  1992/08/25  21:40:44  koziol
  43.  * Initial revision
  44.  *
  45. */
  46. /*****************************************************************************
  47. * Likkai Ng NCSA Feb 92 - update to use H-routines
  48.  
  49. * Likkai Ng May 1991  NCSA 
  50. *
  51. * vsetf.c
  52. * Part of the HDF VSet interface.
  53. *
  54. ************************************************************************/
  55.  
  56. #include "vg.h"
  57.  
  58. /* 
  59. ** ==================================================================
  60. ** PRIVATE data areas and routines
  61. ** ==================================================================
  62. **/
  63.  
  64. /*
  65. stores sizes of local machine's known types 
  66. */
  67.  
  68. PRIVATE int16 local_sizetab[] = 
  69. {
  70.         LOCAL_UNTYPEDSIZE,
  71.         LOCAL_CHARSIZE,
  72.         LOCAL_INTSIZE,
  73.         LOCAL_FLOATSIZE,
  74.         LOCAL_LONGSIZE,
  75.         LOCAL_BYTESIZE,
  76.         LOCAL_SHORTSIZE,
  77.         LOCAL_DOUBLESIZE
  78. };
  79.  
  80. PRIVATE int16 hdf_sizetab[] = 
  81. {
  82.         SIZE_UCHAR8,  /* untyped */
  83.          SIZE_CHAR8,   /* text char */
  84.         SIZE_INT32,   /* maps 16 bits (in mem) to 32 bits (in file) */
  85.          SIZE_FLOAT32, /* float */
  86.          SIZE_INT32,     /* long */
  87.         SIZE_UCHAR8,  /* byte (non-text) */
  88.          SIZE_INT16,     /* short */
  89.         SIZE_FLOAT64  /* double */
  90. };
  91.  
  92. PRIVATE SYMDEF rstab[] = 
  93. {
  94.         "PX", DFNT_FLOAT32,     SIZE_FLOAT32, 1,
  95.         "PY", DFNT_FLOAT32,     SIZE_FLOAT32, 1,
  96.         "PZ", DFNT_FLOAT32,     SIZE_FLOAT32, 1,
  97.  
  98.         "IX", DFNT_INT32,    SIZE_INT32, 1,
  99.         "IY", DFNT_INT32,    SIZE_INT32, 1,
  100.         "IZ", DFNT_INT32,    SIZE_INT32, 1,
  101.  
  102.         "NX", DFNT_FLOAT32,     SIZE_FLOAT32, 1,
  103.         "NY", DFNT_FLOAT32,     SIZE_FLOAT32, 1,
  104.         "NZ", DFNT_FLOAT32,     SIZE_FLOAT32, 1,
  105.  
  106. };
  107.  
  108. #define NRESERVED ( sizeof(rstab)/sizeof(SYMDEF) )
  109. #define LOCALSIZETAB_SIZE sizeof(local_sizetab)/sizeof(int)
  110. #define HDFSIZETAB_SIZE sizeof(hdf_sizetab)/sizeof(int)
  111.  
  112. /*
  113.  ** returns the machine size of a field type
  114.  ** returns FAIL if error
  115.  */
  116. #ifdef PROTOTYPE
  117. int16 SIZEOF (int16 x)
  118. #else
  119. int16 SIZEOF (x)
  120.     int16 x;
  121. #endif
  122. {
  123.   if (x < 0 || x > LOCALSIZETAB_SIZE-1) {
  124.     return(FAIL);
  125.   } else {
  126.     return(local_sizetab[x]);
  127.   }
  128. } /* SIZEOF */
  129.  
  130. /*
  131.  ** returns the HDF file size of a field type
  132.  ** returns FAIL if error.  
  133.  ** USE ONLY FOR BACKWARD COMPATABILITY
  134.  */
  135. #ifdef PROTOTYPE
  136. int16 HDFSIZEOF (int16 x)
  137. #else
  138. int16 HDFSIZEOF (x)
  139.      int16 x; 
  140. #endif
  141. {
  142.   
  143.   if (x<0 || x>HDFSIZETAB_SIZE-1) {
  144.     return(FAIL);
  145.   } else {
  146.     return(hdf_sizetab[x]);
  147.   }
  148. } /* HDFSIZEOF */
  149.  
  150.  
  151. /* ------------------------------------------------------------------ */
  152. /*
  153. ** sets the fields in a vdata for reading or writing
  154. ** RETURNS FAIL if error, and SUCCEED if ok.
  155. ** truncates each field to max length of  FIELDNAMELENMAX.
  156. */
  157.  
  158. #ifdef PROTOTYPE
  159. PUBLIC int32 VSsetfields (VDATA *vs, char *fields)  
  160. #else
  161. PUBLIC int32 VSsetfields (vs,fields)      
  162.      VDATA   *vs;
  163.      char    *fields;
  164. #endif
  165. {
  166.   char  **av;
  167.   int32   ac, found;
  168.   register intn j, i;
  169.   int16   order;
  170.   VREADLIST     rlist;
  171.   VWRITELIST  wlist;
  172.   char  * FUNC = "VSsetfields";
  173.   
  174.   if (vs==NULL) return(FAIL);
  175.   if (scanattrs(fields,&ac,&av) < 0) {
  176.     sprintf(sjs,"@bad fields string [%s]\n",fields);
  177.     zj;
  178.     return(FAIL); /* bad fields string */
  179.   }
  180.   if (ac==0) return(FAIL);
  181.   
  182.   /* 
  183.    * write to an empty vdata : set the write list but do not set the 
  184.    *   read list cuz there is nothing there to read yet...
  185.    */
  186.   if(vs->access == 'w' && vs->nvertices == 0) {
  187.     wlist        = vs->wlist;
  188.     wlist.ivsize = 0;
  189.     wlist.n      = 0;
  190.     for(i = 0; i < ac; i++) {
  191.       /* --- first look in the reserved symbol table --- */
  192.       for(found = 0, j = 0; j < NRESERVED; j++)
  193.         if (!HDstrcmp(av[i], rstab[j].name)) {
  194.           found = 1;
  195.           
  196.           HDstrcpy( wlist.name[wlist.n],rstab[j].name);
  197.           order = rstab[j].order;
  198.           wlist.type[wlist.n]  =  rstab[j].type;
  199.           wlist.order[wlist.n] =  order;
  200.           wlist.esize[wlist.n] =  order * DFKNTsize(rstab[j].type | DFNT_NATIVE);
  201.           wlist.isize[wlist.n] =  order * rstab[j].isize;
  202.           wlist.ivsize  += wlist.isize[wlist.n];
  203.           wlist.n++;
  204.           break;
  205.         }
  206.       
  207.       /* --- now look in the user's symbol table --- */
  208.       if(!found) {
  209.         for(found = 0,j = 0; j < vs->nusym; j++)
  210.           if (!HDstrcmp(av[i], vs->usym[j].name)) {
  211.             found = 1;
  212.             
  213.             HDstrcpy (wlist.name[wlist.n], vs->usym[j].name);
  214.             order = vs->usym[j].order;
  215.             wlist.type[wlist.n]  = vs->usym[j].type;
  216.             wlist.order[wlist.n] = order;
  217.             wlist.esize[wlist.n] = order * DFKNTsize(vs->usym[j].type | DFNT_NATIVE);
  218.             wlist.isize[wlist.n] = order * vs->usym[j].isize;
  219.             wlist.ivsize+= wlist.isize[wlist.n];
  220.             wlist.n++;
  221.             break;
  222.           }
  223.       }
  224.       if (!found) {    /* field is not a defined field - error  */
  225.         sprintf(sjs,"@Vsetfield:field [%s] unknown\n",av[i]); zj;
  226.         return(FAIL);
  227.       }
  228.     }
  229.     
  230.     /* *********************************************************** */
  231.     /* ensure fields with order > 1 are alone  */
  232.     for (j=0,i=0;i<wlist.n;i++)
  233.       if (wlist.order[i] >1 && wlist.n != 1) {
  234.         sprintf(sjs,"@Vsetf: [%s] in [%s] has order %d. error.\n",
  235.                 wlist.name[i], fields, wlist.order[i]); zj;
  236.         return(FAIL);
  237.       }
  238.     /* *********************************************************** */
  239.     
  240.     /* compute and save the fields' offsets */
  241.     for (j=0,i=0;i<wlist.n;i++) {
  242.       wlist.off[i] =j;
  243.       j += wlist.isize[i];
  244.     }
  245.     
  246.     /* copy from wlist (temp) into vdata */
  247.     HDmemcpy((VOIDP) &(vs->wlist), (VOIDP) &(wlist), sizeof(wlist));
  248.     
  249.     return(1); /* ok */
  250.     
  251.   } /* writing to empty vdata */
  252.  
  253.   /*
  254.    *   No matter the access mode, if there are elements in the VData 
  255.    *      we should set the read list
  256.    */
  257.   if(vs->nvertices > 0) {
  258.     rlist   = vs->rlist;
  259.     rlist.n = 0;
  260.     for (i=0;i<ac;i++) {
  261.       for (found=0, j=0; j<vs->wlist.n; j++)
  262.         if (!HDstrcmp(av[i], vs->wlist.name[j]) ) { /*  see if field exist */
  263.           found = 1;
  264.           
  265.           rlist.item[rlist.n] = j; /* save as index into wlist->name */
  266.           rlist.n++;
  267.           break;
  268.         }
  269.       if (!found)  {    /* field does not exist - error */
  270.         sprintf(sjs,"@Vsetfield: field [%s] does not exist in vs\n",
  271.                 av[i]); zj;
  272.         return(FAIL);
  273.       }
  274.     }
  275.     
  276.     /* copy from rlist (temp) into vdata */
  277.     HDmemcpy((VOIDP) &(vs->rlist), (VOIDP) &(rlist), sizeof(rlist));
  278.     
  279.     return(SUCCEED);
  280.     
  281.   } /* setting read list */
  282.  
  283.   return (FAIL);
  284.  
  285. } /* VSsetfields */
  286.  
  287. /* ------------------------------------------------------------------ */
  288. /* 
  289. ** defines a (one) new field within the vdata 
  290. ** return FAIL if error
  291. ** return SUCCEED if success
  292. */
  293.  
  294. #ifdef PROTOTYPE
  295. PUBLIC int32 VSfdefine (VDATA *vs, char *field, int32 localtype, int32 order)   
  296. #else
  297. PUBLIC int32 VSfdefine (vs, field, localtype, order)   
  298.      VDATA   *vs;
  299.      char    *field;
  300.      int32   localtype, order;
  301. #endif
  302. {
  303.   char     **av;
  304.   int32 ac;
  305.   char     *ss;
  306.   int16 usymid, replacesym;
  307.   register intn j;
  308.   char  * FUNC = "VSfdefine";
  309.   
  310.   if ((vs == NULL) || (scanattrs(field,&ac,&av) < 0) || (ac != 1)) {
  311.     HRETURN_ERROR(DFE_ARGS, FAIL);
  312.   }
  313.   
  314.   if (order <1 || order > 1000) {
  315.     sprintf(sjs,"@VSfdefine: error order %ld\n",order);
  316.     zj;
  317.     return(FAIL);
  318.   }
  319.   
  320.   /* 
  321.    ** check for any duplicates 
  322.    */
  323.   /* --- first look in the reserved symbol table --- */
  324.   for (j=0;j<NRESERVED;j++)
  325.     if (!HDstrcmp(av[0], rstab[j].name)) {
  326.       if (localtype != rstab[j].type && order != rstab[j].order) {
  327.         sprintf(sjs,"@VSfdefine warning: predefined field [%s] redefined.\n", av[0]); zj;
  328.         break;
  329.       }
  330.     }
  331.  
  332.   /* --- then look in the user's symbol table --- */
  333.   for (replacesym = 0,j = 0; j < vs->nusym; j++)
  334.     if (!HDstrcmp(av[0], vs->usym[j].name))  {
  335.       if (localtype != rstab[j].type && order != rstab[j].order) {
  336.         sprintf(sjs,"@VSfdefine warning: user field [%s] redefined.\n",av[0]); zj; 
  337.         replacesym = 1;
  338.         break;
  339.       }
  340.     }
  341.   
  342.   if (replacesym)  
  343.     usymid = j; /* new definition will replace old at this index */
  344.   else 
  345.     usymid = vs->nusym;
  346.   
  347.   if ((vs->usym[usymid].isize = DFKNTsize( (int32) localtype)) == FAIL) {
  348.     sprintf(sjs,"@Vfdefine: bad type (=%ld) for [%s]\n", localtype, av[0]); zj;
  349.     return(FAIL);
  350.   }
  351.   
  352.   j  = HDstrlen(av[0]) + 1;
  353.   
  354.   if( (ss = (char*) HDgetspace (j))==NULL)
  355.     HRETURN_ERROR(DFE_NOSPACE, FAIL);
  356.   
  357.   HDstrcpy(ss, av[0]);
  358.   vs->usym[usymid].name  = ss;
  359.   vs->usym[usymid].type  = (int16)localtype;
  360.   vs->usym[usymid].order = (int16)order;
  361.   
  362.   /* prevent user-symbol table overflow */
  363.   if (vs->nusym >= USYMMAX) {
  364.     sprintf(sjs,"@VSFDEFINE: %d too many symbols\n",vs->nusym); zj;
  365.     for(j = 0; j < vs->nusym; j++)
  366.       { sprintf(sjs,"@ sym: %d [%s]\n",j,vs->usym[j].name); zj; }
  367.     return(FAIL);
  368.   }
  369.   
  370.   /* increment vs->nusym only if no user field has been redefined */
  371.   if (!replacesym) vs->nusym++;
  372.   
  373.   return(SUCCEED); 
  374.   
  375. } /* VSfdefine */
  376.  
  377. /* ------------------------------------------------------------------ */
  378.  
  379.